<template>
    <div class="app-container">
        <div class="search-div">
            <el-form label-width="70px" size="small">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="文件名">
                            <el-input style="width: 95%" v-model="searchObj.fileName" placeholder="部分/完整文件名"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="创建时间">
                            <el-date-picker v-model="createTimes" type="datetimerange" range-separator="至"
                                start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"
                                style="margin-right: 10px;width: 100%;" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="display:flex">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="fetch()">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
                </el-row>
            </el-form>
        </div>
        <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">
            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * pageSize + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column prop="fileName" label="文件名" width="180" />
            <el-table-column prop="type" label="文件类型" width="110" />
            <el-table-column prop="size" label="文件大小" />
            <el-table-column prop="createTime" label="创建时间" />

            <el-table-column label="操作" align="center" fixed="right">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.fileId)" title="修改" />
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.fileId)"
                        title="删除" />
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination :current-page="page" :total="total" :page-size="pageSize" style="padding: 30px 0; text-align: center;"
            layout="total, prev, pager, next, jumper" @current-change="fetch" />

        <el-dialog title="修改" :visible.sync="dialogVisible" width="40%">
            <el-form ref="dataForm" :model="file" label-width="100px" size="small" style="padding-right: 40px;">
                <el-form-item label="文件名" prop="fileName">
                    <el-input v-model="file.fileName" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
                <el-button type="primary" icon="el-icon-check" @click="update()" size="small">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import api from '@/api/system/file'
export default {
    data() {
        return {
            listLoading: false,
            list: null,
            total: 0,
            page: 1,
            pageSize: 8,
            searchObj: {}, // 查询表单对象
            createTimes: [],

            dialogVisible: false,
            file: {},
        }
    },
    created() {
        this.fetch();
    },
    methods: {
        fetch() {
            this.searchObj.createTimeBegin = this.createTimes[0]
            this.searchObj.createTimeEnd = this.createTimes[1]
            api.getPageList(this.page, this.pageSize, this.searchObj).then(response => {
                this.list = response.data.records
                this.total = response.data.total
            })
        },
        removeDataById(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                api.removeById(id)
                    .then(response => {
                        //提示
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        //刷新
                        this.fetch()
                    })
            })
        },
        edit(id) {
            this.dialogVisible = true
            api.getUserId(id)
                .then(response => {
                    this.file = response.data
                })
        },
        update() {
            api.update(this.file)
                .then(response => {
                    this.$message.success('操作成功')
                    this.dialogVisible = false
                    this.fetch()
                })
        },
        resetData() {
            this.searchObj = {}
            this.createTimes = []
            this.fetch()
        },
    },

}
</script>

<style></style>